/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services;

import services.interfaces.IGeneric;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaQuery;

/**
 *
 * @author amega
 */
public abstract  class GenericServiceImpl<T>  implements IGeneric<T> {
    
    private Class<T> entityClass;
    
    public GenericServiceImpl(Class<T> entityClass) {
        this.entityClass = entityClass;
    }
    
    protected abstract  EntityManager getEntityManager();
    
    /*
    @Override
    public int count() {
        return (Long);
    }
     * 
     * 
     */

    @Override
    public void remove(T entity) {
        getEntityManager().remove(getEntityManager().merge(entity));
    }

    @Override
    public void edit(T entity) {
        getEntityManager().merge(entity);
    }

    @Override
    public void create(T entity) {
        getEntityManager().persist(entity);
    }

    @Override
    public T find(Long id) {
        return getEntityManager().find(this.entityClass, id);
    }

    @Override
    public List<T> findAll() {
        CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
        cq.select(cq.from(this.entityClass));
        return getEntityManager().createQuery(cq).getResultList();
    }
    
}
